package com.neo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: MsgTtlQueueConfig
 * @Title: springboot-rabbitmq
 * @Package: com.neo.config
 * @Description: 延时队列优化
 * @Author: Kisen
 * @Date: 2021/9/25 16:00
 */
@Configuration
public class MsgTtlQueueConfig {

    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    public static final String QUEUE_C = "QC";

    // 声明队列C 并绑定到对应的死信交换机
    @Bean
    public Queue queueC() {
        Map<String, Object> arguments = new HashMap<>(2);
        // 声明当前队列绑定的死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        // 声明当前队列的死信路由key
        arguments.put("x-dead-letter-routing-key", "YD");
        // 没有声明TTL属性
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }

    // 声明队列C绑定X交换机
    @Bean
    public Binding queuecBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }
}
